import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { FundEntryPageService } from './fund-entry-page.service';
import { FundDetailComponent } from '../../modal/fund-detail/fund-detail.component';
import * as _ from "lodash";

@Component({
  selector: 'app-fund-entry-page',
  templateUrl: './fund-entry-page.component.html',
  styleUrls: ['./fund-entry-page.component.scss'],
  providers: [FundEntryPageService]
})
export class FundEntryPageComponent implements OnInit {

  // 查看明细modal
  @ViewChild("detail", { read: ViewContainerRef })
  detail: ViewContainerRef;
  fundDetailComponent: ComponentRef<FundDetailComponent>;

  public listData: any = [];

  //基金状态下拉框值
  public fundList: any;
  //运行状态
  public sStatusList: any;

  public tabType: any;

  //分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 50,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };

  //列表全选
  public isCheckedAll: any = false;

  //查询列表getBody
  public getBody: any = {
    pageIndex: 1,
    pageSize: 50,
    sFundCode: '',
    sFundName: '',
    sStatus: '',
    sFundType: ''
  };

  constructor(
    public fundEntryService: FundEntryPageService,
    public resolver: ComponentFactoryResolver
  ) { }

  ngOnInit() {
    this.loadlist();
    this.getFundStatus();

  }

  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      // 更新为全选
      _.forEach(this.listData, item => {
        item.isChecked = true;
      });
    } else {
      // 更新为不全选
      _.forEach(this.listData, item => {
        item.isChecked = false;
      });
    }
  }

  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.listData, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.listData.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }

  //分页列表
  loadlist(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    // 查询条件转码
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.fundEntryService.list(this.getBody).subscribe(data => {
      if (data) {
        console.log(data);
        this.listData = data.list;
        this.listPageInfo.currentPageNum = data.pageNum;
        this.listPageInfo.totalPages = data.pages;
        this.listPageInfo.total = data.total;
        this.listPageInfo.startRow = data.startRow;
        console.log(this.listPageInfo.startRow);
        this.listPageInfo.endRow = data.endRow;
      }
    })
  }

  // 条数分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.loadlist();
    }
  }
  // 列表分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.loadlist(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }

  showModal(item) {
    if (this.fundDetailComponent && this.fundDetailComponent.instance) {
      this.fundDetailComponent.destroy();
    }
    // 动态创建组件的实例
    const fundDetailComponent = this.resolver.resolveComponentFactory(FundDetailComponent);
    this.fundDetailComponent = this.detail.createComponent(fundDetailComponent);
    console.log(item);
    this.fundDetailComponent.instance.item = item;
    window['$']('#modal1').modal('show');
  }
  // 获取基金状态下拉框
  getFundStatus() {
    let that = this;
    this.fundEntryService.getFundStatus().subscribe(data => {
      if (data) {
        that.fundList = data.subTree;
        console.log(data)
      }
    })
  }
  //运行状态联动
  sStatusChange() {
    console.log(this.getBody.sFundType - 1)
    this.sStatusList = this.fundList[this.getBody.sFundType - 1].subTree;
  }

}
